#!/bin/sh

# Check decoding of ip:port pairs associated with socket descriptors

. "${srcdir=.}/init.sh"

# strace -yy is implemented using /proc/self/fd
[ -d /proc/self/fd/ ] ||
	framework_skip_ '/proc/self/fd/ is not available'

check_prog awk
check_prog sed

rm -f $LOG.* $LOG-*

./inet-accept-connect-send-recv ||
	fail_ 'inet-accept-connect-send-recv failed'

./netlink_inet_diag || {
	if [ $? -eq 77 ]; then
		framework_skip_ 'NETLINK_INET_DIAG is not available'
	else
		fail_ 'netlink_inet_diag failed'
	fi
}

args="-tt -ff -yy -o $LOG -eclose,network ./inet-accept-connect-send-recv"
$STRACE $args ||
	fail_ "strace $args failed"

"$srcdir"/../strace-log-merge $LOG > $LOG || {
	cat $LOG
	fail_ 'strace-log-merge failed'
}
rm -f $LOG.*

child="$(sed -rn '/SIGCHLD/ s/^.*, si_pid=([1-9][0-9]*), .*/\1/p' $LOG)"
[ -n "$child" ] || {
	cat $LOG
	fail_ 'failed to find pid of child process'
}

sed -rn "/^$child"' /!d; / socket\(/,$ s/^[0-9]+ +[^ ]+ (.+)/\1/p' $LOG > $LOG-connect &&
sed -rn "/^$child"' /d; /SIGCHLD/d; / socket\(/,$ s/^[0-9]+ +[^ ]+ (.+)/\1/p' $LOG > $LOG-accept || {
	cat $LOG
	fail_ 'failed to separate logs'
}

awk -f "$srcdir"/net-yy-connect.awk $LOG-connect || {
	cat $LOG-connect
	fail_ "strace $args failed to decode socket descriptors properly"
}

awk -f "$srcdir"/net-yy-accept.awk $LOG-accept || {
	cat $LOG-accept
	fail_ "strace $args failed to decode socket descriptors properly"
}

exit 0
